package com.baolai.service.impl;

import com.baolai.entity.GoodsUser;
import com.baolai.mapper.GoodsUserMapper;
import com.baolai.result.Result;
import com.baolai.result.ResultUtil;
import com.baolai.service.GoodsUserService;
import com.baolai.tool.Md5PwdTool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Service
public class GoodsUserServiceImpl extends ServiceImpl<GoodsUserMapper, GoodsUser> implements GoodsUserService {
    @Override
    public GoodsUser getUserByAccount(String account) {
        QueryWrapper<GoodsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",account);
        return this.getOne(queryWrapper);
    }

    @Override
    public Result userSign(GoodsUser goodsUser) {
        GoodsUser goodsUser1 = this.getUserByAccount(goodsUser.getAccount());
        if(goodsUser1 != null){
            return ResultUtil.error("已存在该工号");
        }
        goodsUser.setPassword(Md5PwdTool.MD5Pwd(goodsUser.getAccount(),goodsUser.getPassword()));
        if(this.save(goodsUser)){
            return ResultUtil.success("注册成功");
        }
        return ResultUtil.error("注册失败");
    }

    @Override
    public Result userLogin(String account, String password, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper<GoodsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",account).eq("password",Md5PwdTool.MD5Pwd(account,password));
        GoodsUser goodsUser = this.getOne(queryWrapper);
        if(goodsUser == null){
            return ResultUtil.error("账号或密码不正确");
        }else{
            session.setAttribute("user",goodsUser);
            session.setMaxInactiveInterval(3600);
            return ResultUtil.redirect("/backstage/inboundManager");
        }
    }

    @Override
    public IPage<GoodsUser> listUser(Integer current, Integer size) {
        IPage<GoodsUser> page = new Page<>(current,size);
        return this.page(page);
    }

    @Override
    public boolean updateUser(GoodsUser goodsUser) {
        UpdateWrapper<GoodsUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("account",goodsUser.getAccount());
        goodsUser.setPassword(Md5PwdTool.MD5Pwd(goodsUser.getAccount(),goodsUser.getPassword()));
        return this.update(goodsUser,updateWrapper);
    }

    @Override
    public boolean deleteUser(String account) {
        QueryWrapper<GoodsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",account);
        return this.remove(queryWrapper);
    }
}
